Intro

Creating an R Markdown file

Basic components of R Markdown

---
title: 'Literate Programming: Creating reproducible reports using R Markdown'
author: "Zena Lapp"
date: "4/20/2019"
output: html_document
---
```{r}
# Your code here
```

Markdown

Text formatting and bullet points

  • Bold text with double asterisks like this
  • Italicize text with underscores like this
  • Add code-type font with backticks like this
  • Make a list using dashes (-) or asterisks (*)
  • You can make a nubmered list using 1. for each thing in your list (like the example below)

Lists

Steps to generate an R Markdown file:

  1. Open a .Rmd file in RStudio
  2. Write text and code
  3. Compile using Knit!

Including images

  • You can include an image file like this: ![caption](http://url/for/file)
Figure 1: World map

Figure 1: World map

Subscripts and superscripts

  • Subscript: F2
  • Superscript: F2

Math

You can use $ $ and $$ $$ to insert mat equations. Here is an inline example: \(E = mc^2\). Here is a block example: \[y = \mu + \sum_{i=1}^p \beta_i x_i + \epsilon\]

R code chunks and inline code

Output of code will be embedded in the document.

hist_rnorm = function(n){
  nums = rnorm(n)
  hist(nums)
  return(nums)
}
set.seed(0)
n = 10
nums = hist_rnorm(n)

Here I generated 10 random numbers from a \(N(\mu=0,\sigma=1)\) distribution. The mean of the random numbers is 0.358924. The histogram also doesn’t look like a normal distribution. It seems like I need a larger sample size to get a good estimate of the mean for this distribution.

n = 1000
nums = hist_rnorm(n)

Next I generated 1000 random numbers from the same distribution. The mean is -0.0227039, which is much closer to zero, and the histogram looks much more normal.

To compile

Exercise

gapminder = read.csv("https://raw.githubusercontent.com/swcarpentry/r-novice-gapminder/gh-pages/_episodes_rmd/data/gapminder_data.csv")
plot(gapminder$gdpPercap, gapminder$lifeExp)

library(ggplot2)
ggplot(data = gapminder, aes(x = lifeExp, fill = continent)) + geom_histogram(bins = 100) + facet_wrap(~continent)